Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change context by using paths array #75

Closed

Conversation

donaldpipowitch
Copy link

Less supports to additional contexts by using a paths array as an option (see http://lesscss.org/usage/#command-line-usage-options). This is only done, if the default context can't be resolved.

If the file in an @import rule does not exist at that exact location, less will look for it at the location(s) passed to this option. You might use this for instance to specify a path to a library which you want to be referenced simply and relatively in the less files.

It can be used like this:

{
  test: /\.less$/,
  loader: `less?sourceMap&paths[]=${additionalDirectoryPath}`
}

This is a crucial feature to easily integrate webpack into existing projects.

Less supports to additional contexts by using a paths array as an option (see http://lesscss.org/usage/#command-line-usage-options). This is only done, if the default context can't be resolved.

> If the file in an @import rule does not exist at that exact location, less will look for it at the location(s) passed to this option. You might use this for instance to specify a path to a library which you want to be referenced simply and relatively in the less files.

It can be used like this:

```
{
  test: /\.less$/,
  loader: `less?sourceMap&paths[]=${additionalDirectoryPath}`
}
```

This is a crucial feature to easily integrate webpack into existing projects.
@Craga89
Copy link

Craga89 commented May 23, 2016

UPDATE: Looks like this works correctly when paired with the omit-tidle webpack plugin

When using this patch in Windows, I'm getting failures to resolve the paths inside tryResolvingPath due to what looks like a wrong context being passed through for @import statements that reside in any files that are resolved via a paths config. For example:

My file structure is as follows

cc-designer\
  less\
    - base.less
  node_modules\
    - cc-css\
      - less\
        - base.less
        - layout.less
// File: less\base.less
@import 'cc-css/less/base'
// File: node_modules\cc-css\less\base.less
@import 'layout';

Paths config is as follows:

paths: [
   "e:/git/cc-designer/node_modules"
]

Gives an error:

ERROR in ./~/css-loader?sourceMap!./~/less-loader?{"sourceMap":true,"paths":["e:/git/cc-designer/node_modules"]}!./less/base.less
Module build failed: Cannot resolve module 'layout.less' in e:/git/cc-designer/node_modules
 @ e:/git/cc-designer\node_modules\cc-css\less\base.less (line 1, column 0)
 near lines:
   @import 'layout';

 @ ./less/base.less 4:14-194 13:2-17:4 14:20-200

Debugging the code reveals that on Windows, the currentDirectory trying to be resolved is actually e:/git, not the correct e:/Git/cc-designer/node_modules/cc-css/less/ that I'd expect

filename: reference.less
currentDirectory: e:/git/
context: e:/git/cc-designer/node_modules

Side note: using LESS to compile this works fine, so it's definitely something relating to the way the loaderContext resolution is happening (I think). Any clues?

@gjuchault
Copy link

What is the status of this issue ?

@mediafreakch
Copy link

less-loader uses a custom implementation as the file-manager for less. Honestly I don't understand why, can't it just cut all of that and simply provide less with the paths to search for via webpack.config (like in the pull request above)?

@admosity
Copy link

Any reason in particular this hasn't been merged yet? I'm not quite following the issue here.

filipesilva added a commit to filipesilva/angular-cli that referenced this pull request Jan 13, 2017
Add `paths/includePaths` functionality for `sass` and `stylus`.

Similar functionality for less is blocked by
webpack-contrib/less-loader#75.

To add paths, use the new entry in `angular-cli.json` app object:
```
"stylePreprocessorOptions": {
  "includePaths": [
    "style-paths"
  ]
},
```

Fix angular#1791
filipesilva added a commit to filipesilva/angular-cli that referenced this pull request Jan 13, 2017
Add `paths/includePaths` functionality for `sass` and `stylus`.

Similar functionality for less is blocked by
webpack-contrib/less-loader#75.

To add paths, use the new entry in `angular-cli.json` app object:
```
"stylePreprocessorOptions": {
  "includePaths": [
    "style-paths"
  ]
},
```

Fix angular#1791
filipesilva added a commit to filipesilva/angular-cli that referenced this pull request Jan 13, 2017
Add `paths/includePaths` functionality for `sass` and `stylus`.

Similar functionality for less is blocked by
webpack-contrib/less-loader#75.

To add paths, use the new entry in `angular-cli.json` app object:
```
"stylePreprocessorOptions": {
  "includePaths": [
    "style-paths"
  ]
},
```

Fix angular#1791
filipesilva added a commit to filipesilva/angular-cli that referenced this pull request Jan 13, 2017
Add `paths/includePaths` functionality for `sass` and `stylus`.

Similar functionality for less is blocked by
webpack-contrib/less-loader#75.

To add paths, use the new entry in `angular-cli.json` app object:
```
"stylePreprocessorOptions": {
  "includePaths": [
    "style-paths"
  ]
},
```

Fix angular#1791
filipesilva added a commit to filipesilva/angular-cli that referenced this pull request Jan 13, 2017
Add `paths/includePaths` functionality for `sass` and `stylus`.

Similar functionality for less is blocked by
webpack-contrib/less-loader#75.

To add paths, use the new entry in `angular-cli.json` app object:
```
"stylePreprocessorOptions": {
  "includePaths": [
    "style-paths"
  ]
},
```

Fix angular#1791
filipesilva added a commit to filipesilva/angular-cli that referenced this pull request Jan 19, 2017
Add `paths/includePaths` functionality for `sass` and `stylus`.

Similar functionality for less is blocked by
webpack-contrib/less-loader#75.

To add paths, use the new entry in `angular-cli.json` app object:
```
"stylePreprocessorOptions": {
  "includePaths": [
    "style-paths"
  ]
},
```

Fix angular#1791
filipesilva added a commit to filipesilva/angular-cli that referenced this pull request Jan 19, 2017
Add `paths/includePaths` functionality for `sass` and `stylus`.

Similar functionality for less is blocked by
webpack-contrib/less-loader#75.

To add paths, use the new entry in `angular-cli.json` app object:
```
"stylePreprocessorOptions": {
  "includePaths": [
    "style-paths"
  ]
},
```

Fix angular#1791
filipesilva added a commit to filipesilva/angular-cli that referenced this pull request Jan 19, 2017
Add `paths/includePaths` functionality for `sass` and `stylus`.

Similar functionality for less is blocked by
webpack-contrib/less-loader#75.

To add paths, use the new entry in `angular-cli.json` app object:
```
"stylePreprocessorOptions": {
  "includePaths": [
    "style-paths"
  ]
},
```

Fix angular#1791
filipesilva added a commit to angular/angular-cli that referenced this pull request Jan 19, 2017
Add `paths/includePaths` functionality for `sass` and `stylus`.

Similar functionality for less is blocked by
webpack-contrib/less-loader#75.

To add paths, use the new entry in `angular-cli.json` app object:
```
"stylePreprocessorOptions": {
  "includePaths": [
    "style-paths"
  ]
},
```

Fix #1791
MRHarrison pushed a commit to MRHarrison/angular-cli that referenced this pull request Feb 9, 2017
Add `paths/includePaths` functionality for `sass` and `stylus`.

Similar functionality for less is blocked by
webpack-contrib/less-loader#75.

To add paths, use the new entry in `angular-cli.json` app object:
```
"stylePreprocessorOptions": {
  "includePaths": [
    "style-paths"
  ]
},
```

Fix angular#1791
@cvharris
Copy link

how come this hasn't been merged yet?

@jhnns
Copy link
Member

jhnns commented Mar 7, 2017

This has also been discussed at the sass-loader and I totally understand the motivation.

I'm thinking about making the webpack resolution plugin opt-in. It's the source for many errors and often it also makes the build considerably slower (at least Sass, I haven't tested it with Less).

@jhnns
Copy link
Member

jhnns commented Mar 21, 2017

Starting with less-loader 4, you can now choose between the builtin Less resolver or the webpack resolver. If you specify a paths option, it will not use the webpack resolver at all. Take a look at the README for further information.

It decided to do it that way because specifying the paths option and using the webpack resolver doesn't really make sense to me. If you use the webpack resolver, you can have the same behavior as the paths option by adding an absolute path to resolve.modules.

Thx @donaldpipowitch for your efforts. I hope, this change is in your interest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants